package com.elitetyc.eliteim.client.handler.impl;

import com.elitetyc.eliteim.client.command.InnerCommand;
import com.elitetyc.eliteim.client.command.InnerCommandFactory;
import com.elitetyc.eliteim.client.handler.MsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * @author yuanchao.tan
 * @version 1.0
 * @description 内部命令处理器
 * @Date 2021/9/17 14:11
 */
@Component
@Slf4j
public class InnerCommandMsgHandler implements MsgHandler, Ordered {
    @Autowired
    private InnerCommandFactory innerCommandFactory;

    @Autowired
    private ApplicationContext applicationContext;
    @Override
    public boolean supportHandle(String msg) {
        return innerCommandFactory.innerCommandInfoHashMap.containsKey(msg.split(" ")[0]);
    }

    @Override
    public boolean handleMsg(String msg) {
        final String[] commandSplit = msg.split(" ");
        final InnerCommandFactory.InnerCommandInfo commandInfo = innerCommandFactory.innerCommandInfoHashMap.get(commandSplit[0]);
        InnerCommand innerCommand = null;
        try {
            innerCommand = ((InnerCommand) applicationContext.getBean(Class.forName(commandInfo.getClazz())));
            innerCommand.process(msg);
        } catch (ClassNotFoundException e) {
            log.error("error",e);
        }
        return false;
    }

    @Override
    public int getOrder() {
        return 1;
    }


}
